package com.zbkj.front.controller.shop;

import com.github.pagehelper.PageInfo;
import com.zbkj.common.model.merchant.Merchant;
import com.zbkj.common.model.shop.EbGroupPurchase;
import com.zbkj.common.model.user.User;
import com.zbkj.common.request.EbGroupPurchaseRequest;
import com.zbkj.common.response.EbGroupPurchaseGoodsResponse;
import com.zbkj.common.response.EbGroupPurchaseResponse;
import com.zbkj.common.response.EbGroupPurchaseStatisticsVo;
import com.zbkj.common.result.CommonResult;
import com.zbkj.service.service.*;
import com.zbkj.service.util.WeekdayTextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/front/ebGroupPurchase")
@Api(tags = "团购商品管理")
public class EbGroupPurchaseController {
    @Resource
    EbGroupPurchaseService ebGroupPurchaseService;
    @Resource
    EbGroupBuyShopService ebGroupBuyShopService;
    @Resource
    AdOrderAdvertisingService adOrderAdvertisingService;
    @Autowired
    UserService userService;
    @Autowired
    MerchantService merchantService;

    /**
     * 增加团购商品
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "增加团购商品")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public CommonResult<EbGroupPurchase> add(@RequestBody EbGroupPurchaseRequest request) {
        return CommonResult.success(ebGroupPurchaseService.add(request));
    }


    /**
     * 修改团购商品
     *
     * @param ebGroupPurchaseRequest
     * @return
     */
    @ApiOperation(value = "修改团购商品")
    @PostMapping(value = "/update")
    public CommonResult<EbGroupPurchase> update(@RequestBody EbGroupPurchaseRequest ebGroupPurchaseRequest) {
        return CommonResult.success(ebGroupPurchaseService.update(ebGroupPurchaseRequest));
    }

    /**
     * 审核团购商品
     *
     * @param ebGroupPurchaseRequest
     * @return
     */
    @ApiOperation(value = "审核团购商品")
    @PostMapping(value = "/check")
    public CommonResult<EbGroupPurchase> check(@RequestBody EbGroupPurchaseRequest ebGroupPurchaseRequest) {
        return CommonResult.success(ebGroupPurchaseService.check(ebGroupPurchaseRequest));
    }

    /**
     * 上下架
     *
     * @param ebGroupPurchaseRequest
     * @return
     */
    @ApiOperation(value = "上下架")
    @PostMapping(value = "/updateStatus")
    public CommonResult<Integer> updateStatus(@RequestBody EbGroupPurchaseRequest ebGroupPurchaseRequest) {
        ebGroupPurchaseService.updateStatus(ebGroupPurchaseRequest);
        return CommonResult.success(1);
    }

    /**
     * 删除商品
     *
     * @param ebGroupPurchaseRequest
     * @return
     */
    @ApiOperation(value = "删除商品")
    @PostMapping(value = "/updateDel")
    public CommonResult<Integer> updateDel(@RequestBody EbGroupPurchaseRequest ebGroupPurchaseRequest) {
        ebGroupPurchaseService.updateDel(ebGroupPurchaseRequest);
        return CommonResult.success(1);
    }

    /**
     * 追加库存
     *
     * @param ebGroupPurchaseRequest
     * @return
     */
    @ApiOperation(value = "追加库存")
    @PostMapping(value = "/addStock")
    public CommonResult<Integer> addStock(@RequestBody EbGroupPurchaseRequest ebGroupPurchaseRequest) {
        ebGroupPurchaseService.addStock(ebGroupPurchaseRequest);
        return CommonResult.success(1);
    }

    /**
     * 修改排序
     *
     * @param ebGroupPurchaseRequest
     * @return
     */
    @ApiOperation(value = "修改排序")
    @PostMapping(value = "/updateSortNum")
    public CommonResult<Integer> updateSortNum(@RequestBody EbGroupPurchaseRequest ebGroupPurchaseRequest) {
        ebGroupPurchaseService.updateSortNum(ebGroupPurchaseRequest);
        return CommonResult.success(1);
    }

    /**
     * 查询详情
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "查询详情")
    @GetMapping(value = "/get/{id}")
    public CommonResult<EbGroupPurchaseResponse> get(@PathVariable("id") Long id) {
        return CommonResult.success(ebGroupPurchaseService.getById(id));
    }

    @ApiOperation(value = "分页查询列表")
    @PostMapping(value = "/getPage")
    public CommonResult<PageInfo<EbGroupPurchaseResponse>> getPage(@RequestBody EbGroupPurchaseRequest ebGroupPurchaseRequest) {
        User user = userService.getInfo();
        Merchant merchant = merchantService.getPhoneInfo(user.getPhone());
        if (merchant == null) {

            return CommonResult.success(new PageInfo<>());
        }
        ebGroupPurchaseRequest.setMerId(merchant.getId());
        return CommonResult.success(ebGroupPurchaseService.getPage(ebGroupPurchaseRequest));
    }

    /**
     * 查询列表
     *
     * @return
     */
    @ApiOperation(value = "查询列表")
    @PostMapping(value = "/getList")
    public CommonResult<List<EbGroupPurchaseResponse>> getList(@RequestBody EbGroupPurchaseRequest ebGroupPurchaseRequest) {
        User user = userService.getInfo();
        Merchant merchant = merchantService.getPhoneInfo(user.getPhone());
        if (merchant == null) {
            return CommonResult.success(new ArrayList<EbGroupPurchaseResponse>());
        }
        ebGroupPurchaseRequest.setMerId(merchant.getId());
        return CommonResult.success(ebGroupPurchaseService.getList(ebGroupPurchaseRequest));
    }

    /**
     * 查询商家下团购商品列表
     *
     * @return
     */
    @ApiOperation(value = "查询商家下团购商品列表")
    @PostMapping(value = "/shop/getList")
    public CommonResult<List<EbGroupPurchaseResponse>> getShopList(@RequestBody EbGroupPurchaseRequest ebGroupPurchaseRequest) {
        if (ebGroupPurchaseRequest.getShopId() == null) {
            return CommonResult.failed("请选择商家");
        }


        ebGroupPurchaseRequest.setStatus(true);
        return CommonResult.success(ebGroupPurchaseService.getList(ebGroupPurchaseRequest));
    }

    /**
     * 查询商家下团购商品商品详情
     *
     * @return
     */
    @ApiOperation(value = "查询商家下团购商品商品详情")
    @PostMapping(value = "/shop/getGoodsInfo")
    public CommonResult<EbGroupPurchaseGoodsResponse> getGoodsInfo(@RequestBody EbGroupPurchaseRequest ebGroupPurchaseRequest) {
        if (ebGroupPurchaseRequest.getShopId() == null) {
            return CommonResult.failed("请选择商家");
        }
        ebGroupPurchaseRequest.setStatus(true);
        EbGroupPurchaseGoodsResponse goodsInfo = new EbGroupPurchaseGoodsResponse();
        List<EbGroupPurchaseResponse> goodsList = ebGroupPurchaseService.getShopList(ebGroupPurchaseRequest.getShopId().longValue());
        List<EbGroupPurchaseResponse> shopGoodsList = new ArrayList<>();
        goodsInfo.setShopInfo(ebGroupBuyShopService.getById(ebGroupPurchaseRequest.getShopId().longValue()));

        WeekdayTextUtil weekdayTextUtil = new WeekdayTextUtil();
        //循环goodsList id=1 的加入
        for (EbGroupPurchaseResponse goods : goodsList) {
            if (ebGroupPurchaseRequest.getId().equals(goods.getId())) {
                int sale = adOrderAdvertisingService.selectProductCount(goods.getId(), 4);
                goods.setIsSale(sale);
                goodsInfo.setGoodsInfo(goods);
            }
            if (goods.getStatus()) {
                shopGoodsList.add(goods);
            }
            goods.setGroupPurchaseHoursText(weekdayTextUtil.getGroupPurchaseHoursText(goods.getGroupPurchaseBusinessHoursList()));
        }
        goodsInfo.setGoodsList(shopGoodsList);

        return CommonResult.success(goodsInfo);
    }

    @ApiOperation(value = "查询统计信息")
    @GetMapping(value = "/getShopStatistics")
    public CommonResult<EbGroupPurchaseStatisticsVo> getShopStatistics() {
        User user = userService.getInfo();
        Merchant merchant = merchantService.getPhoneInfo(user.getPhone());
        if (merchant == null) {
            return CommonResult.success(new EbGroupPurchaseStatisticsVo());
        }
        return CommonResult.success(ebGroupPurchaseService.getShopStatistics(merchant.getId()));
    }

}